package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public Integer count() {
        return goodsDao.count();
    }

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for (int i = 4; i > intCode.toString().length(); i--) {

            unitCode = "0" + unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    @Override
    public List<Goods> getlistInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        if (page == null) {
            page = 0;
        }
        if (rows == null) {
            rows = 15;
        }
        List<Goods> goods = goodsDao.getlistInventory(page, rows, codeOrName, goodsTypeId);
        return goods;
    }

    @Override
    public Map<String, Object> list(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        HashMap<String, Object> map = new HashMap<>();
        page = page == 0 ? 1:page;
        int offSet = (page - 1) * rows;
        List<Goods> goodsList = goodsDao.getGoodsList(offSet,rows,goodsName,goodsTypeId);
        map.put("rows",goodsList);
        map.put("total",goodsDao.getGoodsCount(goodsName,goodsTypeId));
        return map;
    }

    @Override
    public ServiceVO save(Goods goods) {
        Integer goodsId = goods.getGoodsId();
        if (goodsId!=null){
            goodsDao.update(goods);
        }else {
            goodsDao.insert(goods);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO delete(Integer goodsId) {
        Goods goods = goodsDao.findByGoodsId(goodsId);
        if (goods.getState()==1){
            return new ServiceVO<>(ErrorCode.STORED_ERROR_CODE,ErrorCode.STORED_ERROR_MESS);
        }else if (goods.getState()==2){
            return new ServiceVO<>(ErrorCode.HAS_FORM_ERROR_CODE,ErrorCode.HAS_FORM_ERROR_MESS);
        }else {
            goodsDao.delete(goodsId);
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        HashMap<String, Object> map = new HashMap<>();
        page = page == 0 ? 1:page;
        int offSet = (page - 1) * rows;
        List<Goods> goodsList = goodsDao.getNoInventoryQuantity(offSet,rows,nameOrCode);
        map.put("rows",goodsList);
        map.put("total",goodsDao.getNoInventoryQuantityCount(nameOrCode));
        return map;
    }

    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        HashMap<String, Object> map = new HashMap<>();
        page = page == 0 ? 1:page;
        int offSet = (page -1) * rows;
        List<Goods> goodsList = goodsDao.getHasInventoryQuantity(offSet,rows,nameOrCode);
        map.put("rows",goodsList);
        map.put("total",goodsDao.getHasInventoryQuantityCount(nameOrCode));
        return map;
    }

    @Override
    public ServiceVO saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        Goods goods = goodsDao.findByGoodsId(goodsId);
        goods.setInventoryQuantity(inventoryQuantity);
        goods.setLastPurchasingPrice(purchasingPrice);
        goods.setPurchasingPrice(purchasingPrice);
        goodsDao.update(goods);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO deleteStock(Integer goodsId) {
        Goods goods = goodsDao.findByGoodsId(goodsId);
        if (goods.getState()==2){
            return new ServiceVO<>(ErrorCode.HAS_FORM_ERROR_CODE,ErrorCode.HAS_FORM_ERROR_MESS);
        }
        goods.setInventoryQuantity(0);
        goodsDao.update(goods);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }


}
